import { observable, action, toJS } from 'mobx';
import { ListView, Toast, Tools } from 'weaver-mobile';
import app from '../../apis/app';
import table from '../../apis/table';

const dataSource = new ListView.DataSource({
  rowHasChanged: (row1, row2) => row1 !== row2,
});
const getLabel = Tools.getLabel; 

export default class PowerStore {
  @observable appid = '';
  @observable browserValue = [];
  @observable dataKey = '';
  @observable mainTainDatas = [];
  @observable dataSource = dataSource.cloneWithRows([]);
  @observable current = 1;
  @observable tabIndex = 0;
  @observable loading = false;

  @action
  setState = (params = {}) => {
    Object.keys(params).forEach((key) => {
      this[key] = params[key];
    });
  }

  @action init = appid => {
    this.appid = appid;
    this.mainTainDatas = [];
    this.dataSource = dataSource.cloneWithRows([]);
    this.current = 1;
  }

   @action setState = (params = {}) => {
    Object.keys(params).forEach((key) => {
      this[key] = params[key];
    });
  }

  @action getShareList = appid => {
    this.loading = true;

    app.shareList(appid).then(res => {
      this.dataKey = res.datas;

      this.getMaintainDatas();
    });
  }

  @action getMaintainDatas = () => {
    table.getMaintainDatas({
      dataKey: this.dataKey,
      current: this.current,
      sortParams: []
    }).then(res => {
      this.loading = false;
      this.mainTainDatas = this.mainTainDatas.concat(res.datas);
      this.dataSource = dataSource.cloneWithRows(toJS(this.mainTainDatas));
    })
  }

  @action onEndReached = () => {
    this.current = this.current + 1;
    this.getMaintainDatas();
  }

  @action deleteShare = (appid, checkList) => {

    return new Promise(resolve => {
      app.deleteShare({ appid, ids: checkList.join(',') }).then(res => {
        this.mainTainDatas = this.mainTainDatas.filter(data => !~checkList.indexOf(data.randomFieldId));
        this.dataSource = dataSource.cloneWithRows(toJS(this.mainTainDatas));

        Toast.success(getLabel(523175, '操作成功'), 1);
        resolve();
      })
    });
  }

  @action addShare = data => {
    let params = {};

    params.appid = this.appid;
    params.rightType = data.rightType;
    params.rightTypespan = data.rightTypespan;
    params.shareType = data.sharetype;
    params.shareTypespan = data.sharetypespan;

    if (data.seclevel && params.shareType !== '7') {
      params.sec1 = data.seclevel.split('-')[0];
      params.sec2 = data.seclevel.split('-')[1];
    }

    if (params.shareType === '1') { // 人力资源
      params.content = data.relatedshare;
      params.contentspan = data.relatedsharespan;
      params.shareTypespan = '人员';
    } else if (params.shareType === '2') { // 分部
      params.content = data.relatedshare;
      params.contentspan = data.relatedsharespan;
      params.includesub = '1';
      params.includesubspan = '含下级';
    } else if (params.shareType === '3') { // 部门
      params.content = data.relatedshare;
      params.contentspan = data.relatedsharespan;
      params.includesub = 1;
      params.includesubspan = '含下级';
    } else if (params.shareType === '6') { // 岗位
      params.content = data.relatedshare;
      params.contentspan = data.relatedsharespan;
      params.joblevel = data.joblevel;
      params.joblevelspan = data.joblevelspan;
      params.jobsubcompany = data.jobfield;
      params.jobsubcompanyspan = data.jobfieldspan;
      params.jobdepartment = data.jobfield;
      params.jobdepartmentspan = data.jobfieldspan;
    } else if (params.shareType === '4') { // 角色
      params.content = data.relatedshare;
      params.contentspan = data.relatedsharespan;
      params.rolelevel = data.rolelevel;
    } else if (params.shareType === '7') { // 群组
      params.chatGroup = data.chatGroup;
      params.chatGroupspan = data.chatGroupspan;
    }

    return app.addShareData(params);
  }

  @action canCelClick = () => {

    this.tabIndex = 1;
  }

  @action onTabChange = (tab, index) => {
    this.tabIndex = index;
  }

}